package com.snpic.appaw.module.insure.util.s3;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ShortLinkGenerator {
    public static String generateShortLink(String longUrl) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(longUrl.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String shortLink = number.toString(16);
        while (shortLink.length() < 32) {
            shortLink = "0" + shortLink;
        }
        return shortLink.substring(0, 7);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
//        String url ="http://kyxskyoss.95505.cn:8060/dxtest211202/dianshi.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230406T081749Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=C3A1IK77AP83UUZI8T4K%2F20230406%2F%2Fs3%2Faws4_request&X-Amz-Signature=e67ea693823695e635c3a1c6e703f48da501176d3acf2c78843f56adeb6c399e";
        String longUrl = "https://www.example.com/very/long/url/that/needs/to/be/shortened";
        String shortLink = ShortLinkGenerator.generateShortLink(longUrl);
        System.out.println(shortLink); // 输出： "5ebe229"

    }
}
